package com.hieuzest.citsaw;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements AMapLocationListener {

    public static final String TAG = "MainActivity";

    private ArrayList<Fragment> fragments = new ArrayList();
    private int currentFragment;
    private FloatingActionButton mFabInfo;
    private FloatingActionButton mFabProfile;
    private FloatingActionButton mFabDiscussion;
    private FloatingActionButton mFabSwitch;

    private AMapLocationClient mLocationClient = null;
    private AMapLocationClientOption mLocationOption = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFabInfo = (FloatingActionButton) findViewById(R.id.fab_info);
        mFabProfile = (FloatingActionButton) findViewById(R.id.fab_settings);
        mFabDiscussion = (FloatingActionButton) findViewById(R.id.fab_chat);
        mFabSwitch = (FloatingActionButton) findViewById(R.id.fab_switch);

        initFragments();
        mFabSwitch.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction()
                    .hide(fragments.get(currentFragment))
                    .show(fragments.get(1 - currentFragment))
//                    .replace(R.id.fragment_container, fragments.get(1 - currentFragment))
                    .commit();
            currentFragment = 1 - currentFragment;
        });



//        mLocationClient = new AMapLocationClient(getApplicationContext());
//        mLocationClient.setLocationListener(this);
//        mLocationOption = new AMapLocationClientOption();
//        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//        mLocationOption.setInterval(1000);
//        mLocationClient.setLocationOption(mLocationOption);
//        mLocationClient.startLocation();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    void initFragments() {
        fragments.add(new MapFragment());
        fragments.add(new CameraFragment());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragments.get(0))
                .add(R.id.fragment_container, fragments.get(1))
                .hide(fragments.get(1))
                .commit();
        currentFragment = 0;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        hide();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mLocationClient.stopLocation();
//        mLocationClient.onDestroy();
    }

    private void hide() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                AndroidApplication.getInstance().CurrentY = aMapLocation.getLatitude();
                AndroidApplication.getInstance().CurrentX = aMapLocation.getLongitude();
                AndroidApplication.getInstance().CurrentBearing = aMapLocation.getBearing();
                Log.i(TAG, "x : " + aMapLocation.getLongitude() + " , y : " + aMapLocation.getLatitude() + " , b :" + aMapLocation.getBearing());
            }else {
                Log.e("AmapError","location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }
}

